package com.example.administrator.babymusic.presenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.Image;
import android.widget.GridView;

import com.example.administrator.babymusic.R;
import com.example.administrator.babymusic.constant.ActionConstant;
import com.example.administrator.babymusic.constant.URLConstant;
import com.example.administrator.babymusic.model.IUserModel;
import com.example.administrator.babymusic.model.UserModel;
import com.example.administrator.babymusic.net.STNetTask;
import com.example.administrator.babymusic.ui.adapter.AdapterBase;
import com.example.administrator.babymusic.ui.adapter.GridAdapter;
import com.example.administrator.babymusic.ui.fragment.MoreFragment;
import com.example.administrator.babymusic.ui.myInterface.IMoreFgView;
import com.example.administrator.babymusic.widget.ListMoreFgItem;
import com.example.administrator.babymusic.widget.MyGridItemBean;
import com.google.gson.Gson;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/3 0003.
 */

public class UserPresenter extends BasePresenter implements IUserPresenter, UserModel.onListener{

    private IUserModel mUserModel;

    private IMoreFgView mMoreFgView;

    private List<String> titles;
    private List<Bitmap> imgs;

    protected UserPresenter(Context context) {
        super(context);
    }

    protected UserPresenter() {

        //获取model
        mUserModel = new UserModel();

        //获取MoreFrament的实例，与之形成交互
        mMoreFgView = new MoreFragment();
    }

    @Override
    public void showFgMoreItem(Context context, GridView myGride) {
        this.setBeforeQueryHook(new STNetTask.BeforeQueryHook() {
            @Override
            public void beforeQueryHook() {

            }
        });
        this.setAfterQueryHook(new STNetTask.AfterQueryHook() {
            @Override
            public void afterQueryHook() {

            }
        });
        mUserModel.getMoreFgItem(URLConstant.GET_MORE, this, this.beforeQueryHook, this.afterQueryHook);    //通过网络来获取grid的item
        int layoutResArrays[] = {R.layout.fg_more_list_item};
        List<ListMoreFgItem> list = new ArrayList<>();
        initFgMoreItem(list);
        AdapterBase<ListMoreFgItem> adapter = new GridAdapter(context, layoutResArrays, list);
        myGride.setAdapter(adapter);


    }


    /*测试用例*/
    private void initFgMoreItem(List list){
        /* String[] titles = new String[]
          { "一年级新手包", "儿童幻想推荐", "一年级新手包", "儿童幻想推荐", "一年级新手包", "儿童幻想推荐"};

        int [] imgs =new int []{
           R.drawable.onegrade, R.drawable.childfancying, R.drawable.dinosaur,
           R.drawable.educate, R.drawable.sleepbaby, R.drawable.schoolready,
  };
        for(int i = 0; i < titles.length; i++) {
            list.add(new ListMoreFgItem(imgs[i], titles[i]));
        }*/
        for(int i = 0; i< imgs.size(); i++){
            list.add(new ListMoreFgItem(imgs.get(i), titles.get(i)));
        }
    }

    @Override
    public void onSuccess(String response) {
        Gson gson = new Gson();
        MyGridItemBean item = gson.fromJson(response, MyGridItemBean.class);
        titles = item.titles;
        for(int i = 0; i < item.imageUrl.size(); i++) {
            imgs.add(mUserModel.getHttpBitmap(item.imageUrl.get(i)));
        }

    }

    @Override
    public void onFailure(String errorReason) {

    }
}

